<?php
    require_once ("/mnt/sdcard/www/Includes/simplecms-config.php"); 
    require_once  ("/mnt/sdcard/www/Includes/connectDB.php");

    include("/mnt/sdcard/www/Includes/header.php");

    if (isset($_POST['submit']))
    {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $query = "SELECT id, username, team, name FROM users WHERE username = ? AND password = SHA(?) LIMIT 1";
        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('ss', $username, $password);

        $statement->execute();
        $statement->store_result();

        if ($statement->num_rows == 1)
        {
            $statement->bind_result($_SESSION['userid'], $_SESSION['username'], $_SESSION['team'], $_SESSION['name']);
            $statement->fetch();
            header ("Location: phoneList.php");
        }
        else
        {
            echo "
                <div class='alert alert-danger'>
                    <strong>로그인 실패!</strong> 아이디 또는 비밀번호를 잘못 입력하셨습니다.
                </div>
            ";
        }
    }
 ?>

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <?php
                if (logged_on())
                {
                    $url = "/phoneList.php";
                }
                else
                {
                    $url = "/index.php";
                }
            ?>
            <a class="navbar-brand" href="<?php echo $url ?>">Device Management System</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/index.php">Home</a></li>
                <?php
                    if (logged_on())
                    {
                        echo '
                            <li><a href="/phoneList.php">기기목록</a></li>
                            <li><a href="/approval.php">결재</a></li>
                        ';
                    }
                ?>
                <?php
                    if (is_admin())
                    {
                        echo '
                            <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">관리메뉴<b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="/phoneEnroll.php">기기등록</a></li>
                                    <li><a href="/manage.php">기기관리</a></li>
                                    <!--<li class="divider"></li>
                                    <li class="dropdown-header">Nav header</li>
                                    <li><a href="#">Separated link</a></li>
                                    <li><a href="#">One more separated link</a></li>-->
                                </ul>
                            </li>
                        ';
                    }
                ?>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <?php
                    if (logged_on())
                    {
                        if (is_admin())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(관리자)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                        elseif (is_member())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(정회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";   
                        }
                        else
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(준회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                    }
                    else
                    {
                        echo "
                            <form action='index.php' method='post' class='navbar-form navbar-right'>
                                <div class='form-group'>
                                    <input type='text' name='username' placeholder='아이디' class='form-control'>
                                </div>
                                <div class='form-group'>
                                    <input type='password' name='password' placeholder='비밀번호' class='form-control'>
                                </div>
                                <button type='submit' name='submit' class='btn btn-success'>로그인</button>
                                <a class='btn btn-warning' href='register.php'>회원가입</a></p>
                            </form>
                        ";
                    }
                ?>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</div>

<div class="jumbotron">
    <div class="container">
        <h1>브라우저 안내</h1>
        <p>Internet Explorer로는 정상적인 이용이 어렵습니다. Internet Explorer 외 다른 브라우저를 이용해 주세요.</p>
        <p><a class="btn btn-primary btn-lg" href="http://www.google.com/chrome">Chrome(권장)</a>
        <a class="btn btn-primary btn-lg" href="http://www.mozilla.com/firefox">FireFox</a></p>
    </div>
</div>

<?php 
    include ("/mnt/sdcard/www/Includes/footer.php");
?>